using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Biblioteca.Objetos;
using SEG.Utils;
using SEG.Objetos;
using Pms.Utils;

namespace SEG
{
    public partial class ConfiguracoesLog : System.Web.UI.Page
    {
        Usuario usuAut;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.Count == 0)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                usuAut = (Usuario)Session["usuario"];
                char perm = BiblioPMS.VerifiquePermissao("CONFIGURACOESLOG.ASPX", BiblioPMS.aplicacaoSEG, usuAut.Login);
                if (perm.Equals('N'))
                {
                    Response.Redirect("Inicio.aspx");
                }
            }
        }

        protected void gvConf_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Session == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string hab = ((DataRowView)e.Row.DataItem).Row["log_hab"].ToString();
                    CheckBox cb = ((CheckBox)e.Row.Cells[1].Controls[1]);
                    cb.Checked = hab[0] == 'V';
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvConf.Rows.Count; i++)
                {
                    string valor;
                    CheckBox cb = (CheckBox)gvConf.Rows[i].Cells[1].Controls[1];
                    if (cb.Checked)
                        valor = "V";
                    else
                        valor = "F";

                    ConfLog conf = new ConfLog(ddlAplicacoes.SelectedValue, gvConf.Rows[i].Cells[0].Text, valor);
                    conf.Alterar(usuAut.Login);
                }
                gvConf.DataBind();
                lblErro.CssClass = "filtro";
                lblErro.Text = "Dados atualizados com sucesso.";
                lblErro.Visible = true;
            }
            catch (Exception ex)
            {
                lblErro.CssClass = "erro";
                lblErro.Text = "*Erro: " + ex.Message;
                lblErro.Visible = true;
            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("ConfiguracoesLog.aspx");
        }

        protected void ddlAplicacoes_SelectedIndexChanged(object sender, EventArgs e)
        {
            dsConf.SelectParameters["cod_aplic"].DefaultValue = ddlAplicacoes.SelectedValue;
            gvConf.DataBind();
        }

        protected void ddlAplicacoes_DataBound(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dsConf.SelectParameters["cod_aplic"].DefaultValue = ddlAplicacoes.SelectedValue;
                gvConf.DataBind();
            }
        }
    }
}
